package cn.myfirefly.moming.house.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.myfirefly.moming.common.controller.BaseCommonController;
import cn.myfirefly.moming.common.service.SystemConfigService;
import cn.myfirefly.moming.house.model.SpfTradeContractDetailDto;
import cn.myfirefly.moming.house.model.SpfTradeContractDto;
import cn.myfirefly.moming.house.model.SpfTradeTrackFormDto;
import cn.myfirefly.moming.house.model.SpfTradeTrackFormFilterDto;
import cn.myfirefly.moming.house.model.SpfTradeWageDto;
import cn.myfirefly.moming.house.service.SpfTradeContractService;
import cn.myfirefly.moming.house.service.SpfTradeTrackFormService;
import cn.myfirefly.moming.house.service.SpfTradeWageService;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.annotation.BjuiDataGridFilterDtoWrap;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;
import cn.myfirefly.utils.FileUtil;
import cn.myfirefly.utils.HtmlTemplateUtil;
import cn.myfirefly.utils.MailUtil;
import cn.myfirefly.utils.PropUtil;
import cn.myfirefly.utils.StrUtil;

@Controller
@RequestMapping("spfTradeTrackForm")
public class SpfTradeTrackFormController extends BaseCommonController {

	@Autowired
	private SpfTradeTrackFormService spfTradeTrackFormService;
	
	@Autowired
	private SpfTradeContractService spfTradeContractService;
	
	@Autowired
	private SystemConfigService systemConfigService;
	
	@Autowired
	private SpfTradeWageService spfTradeWageService;
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		return "spftradetrackform/index";
	}
	
	@RequestMapping("edit.do")
	public String edit(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "spftradetrackform/edit";
	}
	
	@RequestMapping("show.do")
	public String show(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("funcId", "spfTradeTrackForm");
		request.setAttribute("contractId", request.getParameter("contractId"));
		return "spftradecontract/show";
	}
	
	@RequestMapping("wagelist.do")
	@ResponseBody
	public PageResultDto<SpfTradeWageDto> wagelist(SpfTradeWageDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String contractId = request.getParameter("contractId");
		if (StrUtil.isBlank(contractId)) {
			return new PageResultDto<SpfTradeWageDto>();
		}
		dto.setTradeContractId(new Integer(contractId));
		PageResultDto<SpfTradeWageDto> result = spfTradeWageService.getList(dto);
		return result;
	}
	
	@RequestMapping("add.do")
	public String add(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "spftradetrackform/add";
	}
	
	@RequestMapping("addSave.do")
	@ResponseBody
	public ResultDto addSave(SpfTradeTrackFormDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		if (dto.getId() != null) {
			return error("请求参数错误");
		}
		warpModifyInfoByName(dto, request, true);
		spfTradeTrackFormService.insert(dto);
		String emails = systemConfigService.getConfigValue(
				PropUtil.use(Const.Config.CONFIG_FILE).getInt(Const.Config.SYSTEM_ID),
				"track_remind_email");
		if (StrUtil.notBlank(emails)) {
			// 发送邮件
			String subject = "创建跟踪单提醒";
			StringBuffer content = new StringBuffer();
			content.append(getLoginUser(request).getUserName());
			content.append("创建了一个跟踪单，请<a href='");
			content.append(PropUtil.use("config.properties").get("clientLogoutUrl"));
			content.append("'>点击这里</a>登录系统进行查看。");
			Map<String, Object> map = new HashMap<>();
			map.put("content", content.toString());
			map.put("data", spfTradeContractService.getSpfTradeContract(dto.getTradeContractId()));
			String html = HtmlTemplateUtil.getHtml(map, FileUtil.getWebPath()+"/email_template", "contract");
			MailUtil.sendMail(emails, subject, html);
		}
		return success("保存成功");
	}
	
	@RequestMapping("editSave.do")
	@ResponseBody
	public ResultDto editSave(SpfTradeTrackFormDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		if (dto.getId() == null) {
			return error("请求参数错误");
		}
		warpModifyInfoByName(dto, request);
		int count = spfTradeTrackFormService.update(dto);
		if (count == -1) {
			return error("修改的数据不存在，请刷新后再试");
		}
		if (count == -2) {
			return error("当前数据状态为已完成或者已取消，不能修改");
		}
		return success("保存成功");
	}
	
	@RequestMapping("list.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto<SpfTradeTrackFormDto> list(SpfTradeTrackFormFilterDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		this.wrapDataAuth(dto);
		PageResultDto<SpfTradeTrackFormDto> result = spfTradeTrackFormService.getList(dto);
		return result;
	}
	
	@RequestMapping("get.do")
	@ResponseBody
	public ResultDto getSpfTradeTrackForm(SpfTradeTrackFormDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		SpfTradeTrackFormDto spfTradeTrackForm = spfTradeTrackFormService.getSpfTradeTrackForm(dto.getId());
		if (!StrUtil.equals(spfTradeTrackForm.getCreateBy(), this.getLoginUser(request).getUserName())) {
			return error("当前数据不属于您，不能编辑");
		}
		return success(spfTradeTrackForm);
	}
	
	@RequestMapping("getDetail.do")
	@ResponseBody
	public ResultDto getContractDetail(HttpServletRequest request, 
			HttpServletResponse response) {
		String id = request.getParameter("id");
		if (StrUtil.isBlank(id)) {
			return error("请求参数错误");
		}
		SpfTradeContractDetailDto dto = spfTradeContractService.getContractDetail(new Integer(id));
		return success(dto);
	}
	
	@RequestMapping("del.do")
	@ResponseBody
	public ResultDto del(SpfTradeTrackFormDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String msg = spfTradeTrackFormService.del(dto.getId());
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("删除成功");
	}
	
	@RequestMapping("lookup.do")
	public String lookup(SpfTradeContractDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		this.wrapDataAuth(dto);
		PageResultDto<SpfTradeTrackFormDto> resultDto = spfTradeTrackFormService.getList(dto);
		request.setAttribute("resultDto", resultDto);
		request.setAttribute("tradeAddress", dto.getTradeAddress());
		request.setAttribute("tradePerson", dto.getTradePerson());
		String tradeContractId = request.getParameter("tradeContractId");
		request.setAttribute("tradeContractId", tradeContractId);
		return "spftradetrackform/lookup";
	}
}
